/******************************************************************************
 * Include files
 ******************************************************************************/
#include "dev.h"



/**
 * @brief 配置RCC
 *
 */
void Uart_RCC_Configuration(void)
{
	//SYSCTRL_HSI_Enable(SYSCTRL_HSIOSC_DIV1);
    InitTick(48000000); /*MHzʱ��Ƶ��*/

    //外设时钟使能
    SYSCTRL_AHBPeriphClk_Enable(DEBUG_UART_GPIO_CLK, ENABLE);
    DEBUG_UART_APBClkENx(DEBUG_UART_CLK, ENABLE);
}

/**
 * @brief 配置GPIO
 *
 */
void Uart_GPIO_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};
    
    GPIO_WritePin(DEBUG_UART_TX_GPIO_PORT, DEBUG_UART_TX_GPIO_PIN,GPIO_Pin_SET);    // 设置TXD的默认电平为高，空闲

    GPIO_InitStructure.Pins = DEBUG_UART_TX_GPIO_PIN;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_Init(DEBUG_UART_TX_GPIO_PORT, &GPIO_InitStructure);

    GPIO_InitStructure.Pins = DEBUG_UART_RX_GPIO_PIN;
    GPIO_InitStructure.Mode = GPIO_MODE_INPUT_PULLUP;
    GPIO_Init(DEBUG_UART_RX_GPIO_PORT, &GPIO_InitStructure);
	
	
	   //NRST
    GPIO_InitStructure.Pins = NRST_GPIO_PIN;
   GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
   GPIO_Init(NRST_GPIO_PORT, &GPIO_InitStructure);

	//LED
   GPIO_InitStructure.Pins = LED_GPIO_PIN;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_Init(LED_GPIO_PORT, &GPIO_InitStructure);
	

     //UART TX RX 复用
    DEBUG_UART_AFTX;
    DEBUG_UART_AFRX;




}

/**
 * @brief 配置UART
 *
 */
void UART_Configuration(void)
{
    UART_InitTypeDef UART_InitStructure = {0};

    UART_InitStructure.UART_BaudRate = DEBUG_UART_BaudRate;
    UART_InitStructure.UART_Over = UART_Over_16;
    UART_InitStructure.UART_Source = UART_Source_PCLK;
    UART_InitStructure.UART_UclkFreq = DEBUG_UART_UclkFreq;
    UART_InitStructure.UART_StartBit = UART_StartBit_FE;
    UART_InitStructure.UART_StopBits = UART_StopBits_1;
    UART_InitStructure.UART_Parity = UART_Parity_No ;
    UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
    UART_InitStructure.UART_Mode = UART_Mode_Rx | UART_Mode_Tx;
    UART_Init(DEBUG_UARTx, &UART_InitStructure);


    UART_ITConfig(DEBUG_UARTx, UART_IT_TC, ENABLE);
    UART_ITConfig(DEBUG_UARTx, UART_IT_RC, ENABLE);
}



void UART_Configuration1(void)
{
    UART_InitTypeDef UART_InitStructure = {0};

    UART_InitStructure.UART_BaudRate = 9600;
    UART_InitStructure.UART_Over = UART_Over_16;
    UART_InitStructure.UART_Source = UART_Source_PCLK;
    UART_InitStructure.UART_UclkFreq = DEBUG_UART_UclkFreq;
    UART_InitStructure.UART_StartBit = UART_StartBit_FE;
    UART_InitStructure.UART_StopBits = UART_StopBits_1;
    UART_InitStructure.UART_Parity = UART_Parity_No ;
    UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
    UART_InitStructure.UART_Mode = UART_Mode_Rx | UART_Mode_Tx;
    UART_Init(DEBUG_UARTx, &UART_InitStructure);


    UART_ITConfig(DEBUG_UARTx, UART_IT_TC, ENABLE);
    UART_ITConfig(DEBUG_UARTx, UART_IT_RC, ENABLE);
}

///**
// * @brief Retargets the C library printf function to the UART.
// *
// */
//PUTCHAR_PROTOTYPE
//{
//    UART_SendData_8bit(DEBUG_UARTx, (uint8_t)ch);

//    while (UART_GetFlagStatus(DEBUG_UARTx, UART_FLAG_TXE) == RESET);

//    return ch;
//}

//size_t __write(int handle, const unsigned char * buffer, size_t size)
//{
//    size_t nChars = 0;

//    if (buffer == 0)
//    {
//        /*
//         * This means that we should flush internal buffers.  Since we
//         * don't we just return.  (Remember, "handle" == -1 means that all
//         * handles should be flushed.)
//         */
//        return 0;
//    }


//    for (/* Empty */; size != 0; --size)
//    {
//        UART_SendData_8bit(DEBUG_UARTx, *buffer++);
//        while (UART_GetFlagStatus(DEBUG_UARTx, UART_FLAG_TXE) == RESET);
//        ++nChars;
//    }

//    return nChars;
//}



